#include<stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <windows.h>

#define high 20//画面尺寸 
#define width 50
//全局变量

int canvas[high][width]={0};//对应元素  0为空格 -1为边框 1为蛇头@ >1为蛇身*  -2为食物
int posx,posy;//蛇头坐标 
int movedirection;//小蛇移动方向1,2,3,4，上下左右 
int food_x,food_y;//食物位置

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info);
}

void gotoxy(int x,int y)
{
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(handle,pos);
}

void startup()
{
	HideCursor(); 
	
	int i,j;
	
	for(i=0;i<high;i++)//边框 
	{
		canvas[i][0]=-1;
		canvas[i][width-1]=-1;
	 } 
	 for(j=0;j<width;j++)
	{
		canvas[0][j]=-1;
		canvas[high-1][j]=-1;
	 } 
	 
	 canvas[high/2][width/2]=1;//蛇头，蛇身 
	 for(i=1;i<=4;i++)
	 {
	 	canvas[high/2][width/2-i]=i+1;
	  } 
	  
	 movedirection=1; //运动方向

	 food_x=rand()%(high-5)+2;//食物
	 food_y=rand()%(width-5)+2;
	 if(canvas[food_x][food_y]>=1)
	 {
		food_x=rand()%(high-5)+2;//食物
		food_y=rand()%(width-5)+2;
	 }
	 canvas[food_x][food_y]=-2;
}

void show()
{
	gotoxy(0,0);
	
		int i,j;
	for(i=0;i<high;i++)
	{
		for(j=0;j<width;j++)
		{
			if(canvas[i][j]==0)
			{
				printf(" ");
			}
			else if(canvas[i][j]==-1)
			{
				printf("#");              //输出边框 
			}
			else if(canvas[i][j]==1)
			{
				printf("@");              //输出蛇头 
			}
			else if(canvas[i][j]>1)
			{
				printf("*");              //输出蛇身
			}
			else if(canvas[i][j]==-2)
			{
				printf("$");              //输出食物
			}
			else
			{
				printf(" ");
			}
		}
		printf("\n");
	 } 
	Sleep(150);
}

void updatewithinput()
{
	
	char input;
	if(kbhit())
	{
		input=getch();
		
		if(input=='a')
		{
			movedirection=3;
		}
		if(input=='d')
		{
			movedirection=4;
		}
		if(input=='w')
		{
			movedirection=1;
		}
		if(input=='s')
		{
			movedirection=2;
		}
		if(input==27)
		{
			system("pause");
		}	
	}	
}

void movesnakebydirection()
{
	int i,j;
	int max=0;
	int oldtail_i,oldtail_j;
	int oldhead_i,oldhead_j;
	 
	for(i=1;i<high-1;i++)
	{
		for(j=1;j<width-1;j++)
		{
			if(canvas[i][j]>0) 
			{
				canvas[i][j]++;//对所有大于零的元素加一
				
				if(max<canvas[i][j])//求出最大值 
				{
					max=canvas[i][j];
					oldtail_i=i;
					oldtail_j=j;
				}
				
				if(canvas[i][j]==2)//记录旧蛇头位置 
				{
					oldhead_i=i;
					oldhead_j=j;
				}
			}
		}
	}
	
	int newhead_i,newhead_j;

	if(movedirection==1)
	{
		//上下左右 
		newhead_i=oldhead_i-1;
		newhead_j=oldhead_j;
	} 
	 if(movedirection==2)
	{
		;
		newhead_i=oldhead_i+1;
		newhead_j=oldhead_j;
	 } 
	 if(movedirection==3)
	{
		
		newhead_i=oldhead_i;
		newhead_j=oldhead_j-1;
	 }
	 if(movedirection==4)
	{
		
		newhead_i=oldhead_i;
		newhead_j=oldhead_j+1;
	 }  

	 //判断是否吃到食物
	 if(canvas[newhead_i][newhead_j]==-2)
	{
		canvas[food_x][food_y]=0;
		food_x=rand()%(high-2)+1;//食物
		food_y=rand()%(width-2)+1;
		if(canvas[food_x][food_y]>=1)
		{
			food_x=rand()%(high-2)+1;//食物
			food_y=rand()%(width-2)+1;
		}
		else
		{
			canvas[food_x][food_y]=-2;
		}

		
	}
	else
	{
			canvas[oldtail_i][oldtail_j]=0;//最大值所在元素变为零 	
	}


	

	 //撞击判断
	 if(canvas[newhead_i][newhead_j]>0||canvas[newhead_i][newhead_j]==-1)
	 {
		 printf("game over!!!\n");
		 exit(0);
	 }
	 else
	 {
		canvas[newhead_i][newhead_j]=1;
	 }

 } 
 
void updatewithoutinput()
{
	
	movesnakebydirection();
}



int main()
{
	startup();
	
	while(1)
	{
		show();
		updatewithinput();
		updatewithoutinput();
		
	}	
	return 0;
 } 